# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.

# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("ts_scoreengine2sdk")

# Sets C/C++ standard
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Source and header directories
set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)

# Sets output directory
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../../build/intermediates/cmake/release/obj/${ANDROID_ABI})

# Android specific settings
add_definitions(-DANDROID)

# Main library sources
set(SCORE_ENGINE_SOURCES
    # Main sources
    ${SRC_DIR}/preprocess.c
    ${SRC_DIR}/feature_extractor.c
    ${SRC_DIR}/score_engine.c
    ${SRC_DIR}/calculate_score.c
    ${SRC_DIR}/technique_score.c
    ${SRC_DIR}/base_file_parser.c
    
    # Algorithm modules
    ${SRC_DIR}/algorithm/utils.c
    ${SRC_DIR}/algorithm/pitch/cfftn.c
    # WebRTC VAD sources
    ${SRC_DIR}/algorithm/webrtcvad/webrtc_vad.c
    ${SRC_DIR}/algorithm/webrtcvad/vad_core.c
    ${SRC_DIR}/algorithm/webrtcvad/vad_sp.c
    ${SRC_DIR}/algorithm/webrtcvad/vad_filterbank.c
    ${SRC_DIR}/algorithm/webrtcvad/vad_gmm.c
    ${SRC_DIR}/algorithm/webrtcvad/signal_processing_library.c
    
    # JNI interface files
        ${CMAKE_CURRENT_SOURCE_DIR}/scoreengine2sdk.cpp
)

# Header directories
set(INCLUDE_DIRS
    ${INCLUDE_DIR}
    ${SRC_DIR}
    ${SRC_DIR}/algorithm
    ${SRC_DIR}/algorithm/webrtcvad/include
)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
#
# In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
# the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
# is preferred for the same purpose.
#
# In order to load a library into your app from Java/Kotlin, you must call
# System.loadLibrary() and pass the name of the library defined here;
# for GameActivity/NativeActivity derived applications, the same library name must be
# used in the AndroidManifest.xml file.
add_library(${CMAKE_PROJECT_NAME} SHARED ${SCORE_ENGINE_SOURCES})

# Adds header directories
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${INCLUDE_DIRS})

# Adds compile options
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE -g -O0 -Wall -Wextra)
else()
    target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE -O2 -Wall -Wextra)
endif()

# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
target_link_libraries(${CMAKE_PROJECT_NAME}
        # List libraries link to the target library
        android
        log)

# Sets output properties
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
    OUTPUT_NAME "ts_scoreengine2sdk"
    VERSION 1.0.0
)